package design.mode.effective_java.chapter3.item10;

/**
 * @author ZhongMingde on 2021/8/11
 */

public class PhoneNumber {
    private final short areaCode,prefix,lineNum;

    public PhoneNumber(short areaCode, short prefix, short lineNum) {
        this.areaCode = rangeCheck(areaCode,999,"area code");
        this.prefix = rangeCheck(prefix,999,"prefix");
        this.lineNum = rangeCheck(lineNum,9999,"lineNum");
    }

    private static short rangeCheck(int val, int max,String arg)  {
        if (val < 0 || val > max){
            throw new IllegalArgumentException(arg + ": " + val);
        }
        return (short) val;
    }


    @Override
    public boolean equals(Object o){
        if (o == this){
            return true;
        }
        if (!(o instanceof  PhoneNumber)){
            return false;
        }
        PhoneNumber pn = (PhoneNumber) o;
        return pn.lineNum == lineNum && pn.prefix == prefix && pn.areaCode == areaCode;
    }
}
